/*
 * ---------------------------------------------------
 * 
 *		 Author: Dmitri
 *       EasyMenu v0.7.0
 *       GNU General Public License v2 (GPL v2)
 *       
 *----------------------------------------------------       
 * 
 *   there is plans to write this version of menu
 *   on JQuery as well, but for now it is only exist
 *   in JavaScript Core + DOM
 *   
 *   So far this menu was tested on Chrome, IE7, IE8
 *   and FireFox
 *   
 *   There is known issue with IE6 with rendering divs 
 *   and CSS properly.
 *   
 *   Note: The pans for this menu to make releases and 
 *   	   improvements quite oftem 
 * 
 * ---------------------------------------------------
 */

JSEasyMenu =
{
	// intitialization getting menues and adding event listeners
	init: function(){
	
		JSEasyMenu.hideAllSubMenu();
		
		var menuItem = document.getElementById("menu_wrap").getElementsByTagName("div");
		
		for (var index = 0; index < menuItem.length; index++) {
			//if(window.addEventListener)
			//if firefox and all oother browsers
			if (typeof menuItem[index].addEventListener != "undefined") {
				menuItem[index].addEventListener("mouseover", JSEasyMenu.ShowListener, false);
				menuItem[index].addEventListener("click", JSEasyMenu.ShowListener, false);
			}
			else if (typeof menuItem[index].attachEvent != "undefined") //IE
			{
					menuItem[index].attachEvent("onclick", JSEasyMenu.ShowListener);
					menuItem[index].attachEvent("onmouseover", JSEasyMenu.ShowListener);
			}
		}
	},
	
	
	//Show sub menu event listener
	ShowListener: function (event){
		JSEasyMenu.hideAllSubMenu();
		
		//var menuItemIdStr = this.id; // thats doesn't work in IE
		var target = event.srcElement ? event.srcElement : event.target;
		JSEasyMenu.showSubMenu(target);
		
	},
	
	
	// show submenu function
	showSubMenu: function(target){
		var menuItemIdStr = target.id;
		
		var menuItemIdNum = menuItemIdStr.replace(/menu/i, "");
		var submenuId = "submenu" + menuItemIdNum;
		
		// if we have sub menu
		if (document.getElementById(submenuId)) {
			var subMenu = document.getElementById(submenuId);
			
			subMenu.style.left = target.offsetLeft + "px";
			subMenu.style.top = target.offsetTop + target.offsetHeight + "px";
			subMenu.style.display = "block";
		}
	},
	
	// hidding all the submenues
	hideAllSubMenu: function (){
		var submenu = document.getElementsByTagName("div");
		for (var i = 0; i < submenu.length; i++) {
			if (submenu[i].className == "submenu_wrap") 
				submenu[i].style.display = "none";
		}
	}
};